﻿using AutoMapper;
using PeiYue.Common.Helper;
using PeiYue.IRepository;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class RouterServices : BaseServices<RouterDao>, IRouterServices
    {
        IBaseRepository<RouterDao> _dal;
        IMapper _mapper;
        public RouterServices(IBaseRepository<RouterDao> dal, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取线路分页列表
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        public async Task<PageModel<SearchRouterResponseDto>> GetPage(SearchRouterRequestDto requestDto)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select r.*,u1.namec as create_names,u2.namec as update_names,w.names as wh_name, ");
            strSql.Append("case when t.id>0 then 1 else 0 end task_status ");
            strSql.Append("from oms_wms_router r ");
            strSql.Append("left join oms_ur_user u1 on r.create_user=u1.id ");
            strSql.Append("left join oms_ur_user u2 on r.update_user=u2.id ");
            strSql.Append("left join oms_sys_warehouse w on r.wh_id=w.id ");
            strSql.Append("left join oms_tms_task_header t on t.lineid=r.id and t.status=1 and Convert(varchar(10),t.create_time,120)=Convert(varchar(10),getdate(),120) ");
            strSql.Append("where w.status=1 ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            if (requestDto.wh_id.HasValue && requestDto.wh_id > 0)
            {
                strSql.Append("and r.wh_id=@wh_id ");
                parameters.Add(new SugarParameter("@wh_id", requestDto.wh_id));
            }
            if (requestDto.status.HasValue && requestDto.status > 0)
            {
                strSql.Append("and r.status=@status ");
                parameters.Add(new SugarParameter("@status", requestDto.status));
            }
            else
            {
                strSql.Append("and r.status<3 ");
            }
            if (!string.IsNullOrWhiteSpace(requestDto.names))
            {
                strSql.Append("and r.names like @names ");
                parameters.Add(new SugarParameter("@names", "%" + requestDto.names + "%"));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.codec))
            {
                strSql.Append("and r.codec like @codec ");
                parameters.Add(new SugarParameter("@codec", "%" + requestDto.codec + "%"));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timef))
            {
                strSql.Append("and r.create_time>=@create_time_begin ");
                parameters.Add(new SugarParameter("@create_time_begin", requestDto.timef));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timet))
            {
                strSql.Append("and r.create_time<@create_time_end ");
                parameters.Add(new SugarParameter("@create_time_end", Convert.ToDateTime(requestDto.timet).AddDays(1)));
            }
            return await base.SqlQueryPage<SearchRouterResponseDto>(strSql.ToString(), parameters, requestDto.pageIndex, requestDto.pageSize, " r.id desc ");
        }
    }
}
